﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using IAM.Interface.Common.Abnormity;
using IAM.Interface.Common.Context;

namespace ARSys.Web
{
    /// <summary>
    /// Summary description for ITPortalSync
    /// </summary>
    public class ITPortalSync : IHttpHandler
    {
        private HttpContext context;

        public void ProcessRequest(HttpContext context)
        {
            this.context = context;


            switch (Request.HttpMethod)
            {
                case "GET":
                    Response.Write("IAM is running, please POST this page to submit any request.");
                    break;
                case "POST":
                    TransactData();
                    break;
                default:
                    throw new NotImplementedException(String.Format("HttpMethod {0} was not supported", Request.HttpMethod));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region 数据处理
        private HttpRequest Request { get { return context.Request; } }
        private HttpResponse Response { get { return context.Response; } }

        /// <summary>
        /// 数据处理
        /// 0.实例化响应端发送消息上下文控制对象
        /// 0.1判断请求ip是否安全
        /// 1.接收xml请求
        /// 1.1加密类型
        /// 1.2密钥
        /// 1.3xml信息
        /// 2.实例化ResponseContextModel
        /// 4.响应端处理，返回xml包
        /// 5.回写数据流
        /// </summary>
        private void TransactData()
        {
            try
            {

                //实例化响应端发送消息上下文控制对象
                ResponseContextControl rcc = new ResponseContextControl();
                //0.1判断请求ip是否安全
                if (bool.Parse(ConfigurationManager.AppSettings["ValidateIP"]))
                {
                    bool validate = rcc.ValidateRequestIP();
                    if (!validate)
                    {
                        ResponseException rex = new ResponseException();
                        rex.StatusCode = 418;
                        throw (rex);
                    }
                }
                //1.接收xml请求
                //1.3xml信息
                string xmlinfo = Request.Form["xml"];
                string key = ResponseContextControl.GetConfigKey();
                //1.4 签名 
                string sign = "";

                sign = Request.Form["sign"];
                //2.实例化ResponseContextModel
                ResponseContextModel rcm = new ResponseContextModel(
                                                                    sign,
                                                                    0,
                                                                    key,
                                                                    xmlinfo);
                //3.实例化响应端发送消息上下文控制对象
                rcc.ContextModel = rcm;


                //4.响应端处理，返回xml包
                string strresult = rcc.ContextControl();
                //5.回写数据流
                Response.Write(strresult);
            }
            catch (ResponseException re)
            {
                throw (re);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        #endregion

    }
}